package com.example.service.shopping;


import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.exception.CustomException;
import com.example.mapper.shopping.MedicinesMapper;
import com.example.mapper.shopping.MedicinesStyleMapper;
import com.example.mapper.shopping.MedicinesTypeMapper;
import com.example.model.dto.HospitalDto;
import com.example.model.dto.MedicinesDto;
import com.example.model.entity.medical.Hospital;
import com.example.model.entity.other.Account;
import com.example.model.entity.shopping.Medicines;
import com.example.model.entity.shopping.MedicinesStyle;
import com.example.model.entity.shopping.MedicinesType;
import com.example.model.vo.MedicinesVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 药品信息表业务处理
 **/
@Service
public class MedicinesService {

    @Resource
    private MedicinesMapper medicinesMapper;
    @Resource
    private MedicinesStyleMapper medicinesStyleMapper;
    @Resource
    private MedicinesTypeMapper medicinesTypeMapper;

    /**
     * 新增
     */
    public void add(Medicines medicines) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            medicines.setBusinessId(currentUser.getId());
        }
        medicinesMapper.insert(medicines);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        medicinesMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            medicinesMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Medicines medicines) {
        medicinesMapper.updateById(medicines);
    }

    /**
     * 根据ID查询
     */
    public Medicines selectById(Integer id) {
        return medicinesMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<MedicinesVO> selectAll(Medicines medicines) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            medicines.setBusinessId(currentUser.getId());
        }
        List<MedicinesVO> medicinesVOList = medicinesMapper.selectAll(medicines);
        medicinesVOList.forEach(medicinesVO -> {
            Integer medicinesId = medicinesVO.getId();
            List<MedicinesStyle> medicinesStyleList = medicinesStyleMapper.selectByMedicinesId(medicinesId);
            medicinesVO.setMedicinesStyleList(medicinesStyleList);
        });
        medicinesVOList.sort((o1, o2) -> o2.getCount() - o1.getCount());
        return medicinesVOList;
    }

    /**
     * 分页查询
     */
    public PageInfo<Medicines> selectPage(Medicines medicines, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            medicines.setBusinessId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MedicinesVO> voList = medicinesMapper.selectAll(medicines);
        return PageInfo.of(voList);
    }

    public void updateBatch(MedicinesDto medicinesDto) {
        for (Integer id : medicinesDto.getIds()) {
            String status = medicinesDto.getStatus();
            Medicines medicines = medicinesMapper.selectById(id);
            medicines.setStatus(status);
            medicinesMapper.updateById(medicines);
        }
    }

    public MedicinesVO selectDetailById(Integer medicinesId) {
        return medicinesMapper.selectDetailById(medicinesId);
    }
    public List<MedicinesVO> selectByBusinessId(Integer businessId) {
        return medicinesMapper.selectDetailByBusinessId(businessId);
    }

    public List<MedicinesVO> selectByTypeId(Integer typeId) {
        if (typeId == null || typeId <= 0) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        List<MedicinesVO> medicinesVOList = medicinesMapper.selectByTypeId(typeId);
        medicinesVOList.forEach(medicinesVO -> {
            Integer medicinesId = medicinesVO.getId();
            List<MedicinesStyle> medicinesStyleList = medicinesStyleMapper.selectByMedicinesId(medicinesId);
            medicinesVO.setMedicinesStyleList(medicinesStyleList);
        });
        medicinesVOList.sort((o1, o2) -> o2.getCount() - o1.getCount());
        return medicinesVOList;
    }
    public List<MedicinesVO> selectByTypeName(String typeName) {
        MedicinesType medicinesType = medicinesTypeMapper.selectByName(typeName);
        List<MedicinesVO> medicinesVOList = medicinesMapper.selectByTypeId(medicinesType.getId());
        return getMedicinesVOS(medicinesVOList);
    }


    public List<MedicinesVO> getMedicinesVOS(List<MedicinesVO> medicinesVOList) {
        medicinesVOList.forEach(medicinesVO -> {
            Integer medicinesId = medicinesVO.getId();
            List<MedicinesStyle> medicinesStyleList = medicinesStyleMapper.selectByMedicinesId(medicinesId);
            medicinesVO.setMedicinesStyleList(medicinesStyleList);
        });
        medicinesVOList.sort((o1, o2) -> o2.getCount() - o1.getCount());
        return medicinesVOList;
    }


    public Integer selectMedicinesByName(String name) {
        return medicinesMapper.selectMedicinesByName(name);
    }
}
